package com.dsteiner.gwt.cafeteriasophia.client;


import com.dsteiner.gwt.cafeteriasophia.client.ChangesWidget.ChangesModel;
import com.dsteiner.gwt.cafeteriasophia.client.controller.ShiftChangeEditController;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Product;
import com.dsteiner.gwt.cafeteriasophia.client.model.ProductModel;
import com.dsteiner.gwt.cafeteriasophia.client.model.ShiftChangeModel;
import com.dsteiner.widgets.client.PrefilledTextBox;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.IncrementalCommand;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class ShiftChangeEditWidget extends LazyWidget {

	private ShiftChangeEditController shiftChangeController = new ShiftChangeEditController();
	private final ShiftChangeModel shiftChangeModel;

	public ShiftChangeEditWidget(ShiftChangeModel shiftChangeModel) {
		this.shiftChangeModel = shiftChangeModel;
		setStyleName("shift-change-widget");
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		PrefilledTextBoxModel dateTextBoxModel = new PrefilledTextBoxModel();
		dateTextBoxModel.setDefaultText("ingrese fecha");
		PrefilledTextBox dateTextBox = new PrefilledTextBox(dateTextBoxModel);
		HTML dateHtml = new HTML("Fecha del cambio del turno");
		PrefilledTextBoxModel employeeTextBoxModel = new PrefilledTextBoxModel();
		employeeTextBoxModel.setDefaultText("ingrese nombre");
		PrefilledTextBox employeeTextBox = new PrefilledTextBox(employeeTextBoxModel);
		HTML employeeHtml = new HTML("Encargada del turno pasado");
		final FlowPanel contentPanel = new FlowPanel();
		
		mainPanel.add(dateHtml);
		mainPanel.add(dateTextBox);
		mainPanel.add(employeeHtml);
		mainPanel.add(employeeTextBox);
		mainPanel.add(contentPanel);
		
		DeferredCommand.addCommand(new IncrementalCommand(){
			int i = 0;
			public boolean execute() {
				if(i >= Product.products.length)
					return false;
				ProductModel productModel = new ProductModel();
				productModel.setProduct(Product.products[i]);
				ProductWidget productWidget = new ProductWidget(productModel);
				shiftChangeController.registerProductModel(productModel);
				contentPanel.add(productWidget);
				i++;
				return true;
			}
		});

		
		shiftChangeController.registerEmployeeTextBoxModel(employeeTextBoxModel);
		shiftChangeController.registerDateTextBoxModel(dateTextBoxModel);
		shiftChangeController.registerShiftChangeWidget(this);
		shiftChangeController.registerShiftChangeModel(shiftChangeModel);
		shiftChangeController.registerEventManager(EventManagerImpl.getInstance());
		return mainPanel;
	}

}